<?php

/**
 * Class Xu ly thong thong tin loai danh muc
 */
class System_ListTypeController extends Zend_Controller_Action {

    public function init() {
		$session = new Clound_Session();
		if(!$session->checkPermission('160020')){
			$urlLogout = $this->_request->getBaseUrl().'/index/logout';
			header("Location:$urlLogout");
		}
        //Load cau hinh thu muc trong file config.ini
        $app = Zend_Registry::get('App');
        //Lay duong dan thu muc goc (path directory root)
        $this->view->baseUrl = $baseUrl = $this->_request->getBaseUrl() . "/public/";
        $result = array(
                    array($baseUrl.'lib-js/system/admin/System_Admin_Listtype.js','js')
                    ,array($baseUrl.'lib-js/tabs.js','js')

                    ,array($baseUrl.'lib-js/system/upload/css/bootstrap.css','css')
                    ,array($baseUrl.'lib-js/system/upload/css/jquery.fileupload.css','css')
                    ,array($baseUrl.'lib-js/system/upload/css/jquery.fileupload-ui.css','css')
                    ,array($baseUrl.'lib-js/system/upload/js/vendor/jquery.ui.widget.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/tmpl.min.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.iframe-transport.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.fileupload.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.fileupload-process.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.fileupload-ui.js','js')
                );                  
        if ($this->_request->isXmlHttpRequest()){
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'partial'
            ));
            $this->view->arrJsCss = Zend_Json::encode($result);
        }else{
            //Cau hinh cho Zend_layout
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'index'
            ));
            //Load ca thanh phan cau vao trang layout (index.phtml)
            $response = $this->getResponse();
            //Dinh nghia current modul code
            $this->view->currentModulCode = "SYSTEM";
            $this->view->currentModulCodeForLeft = "listtype";
            $this->view->postHeader = Clound_String::registerFile($result);
            // echo  Clound_String::registerFile($result); die;
            //Hien thi file template
            $response->insert('menu', $this->view->renderLayout('menu.phtml', './application/layout/scripts/'));     //Hien thi header 
            $response->insert('submenu', $this->view->renderLayout('submenu.phtml', './application/layout/scripts/'));     //Hien thi header 
            $response->insert('footer', $this->view->renderLayout('footer.phtml', './application/layout/scripts/'));   //Hien thi f
        }
    }

    // Xu ly thong tin de day ra file index.phtml
    public function indexAction() {
    }


    public function saveAction() {
        // Tao doi tuong modeListType
        $model = new System_Models_Listtype();
        //Lay cac tham so cua form
        $sCode = trim(htmlspecialchars($this->_request->getParam('Code')));
        $sName = trim(htmlspecialchars($this->_request->getParam('Name')));
        $iOrders = trim($this->_request->getParam('Orders'));
        $iListtypeID = (int) $this->_request->getParam('ListtypeID');
        $iStatus = $this->_request->getParam('Status');
        $sOwnerCodeList = $this->_request->getParam('OwnerCodeList');
        $sXmlFileName = $this->_request->getParam('XmlFileName');
        if($sOwnerCodeList)
            $sOwnerCodeList = implode(',',$sOwnerCodeList);
        else $sOwnerCodeList = '';
        $iCookieStatus = $this->_request->getParam('CookieStatus');
        $data = array(
            "ListtypeID"=> $iListtypeID,
            "Code"=> $sCode,
            "Name"=> $sName,
            "Orders"=> $iOrders,
            "Status"=> $iStatus,
            "OwnerCodeList"=> $sOwnerCodeList,
            "CookieStatus"=> $iCookieStatus,
            "XmlFileName"=>$sXmlFileName,
        );
        $errors = array();
        if($model->isValid($data)){
            if ($iListtypeID === 0) {
                $model->insert($data);
            } else {
                $where="ListtypeID ='$iListtypeID'";
                $model->update($data,$where);
            }
            if($iOrders){
                $model->reOrder($iListtypeID,$iOrders);
            }
        }else{
            array_push($errors,array('Code'=>'Đã tồn tại'));
        }
        // echo $model->getAdapter()->lastInsertId();
        echo Zend_Json::encode($errors);
        die;
        $this->_helper->getHelper('viewRenderer')->setNoRender();
    }



    /**
     * @see : Thuc hien viec xoa Mot hoac Nhieu Loai Danh muc
     */
    public function deleteAction() {
        $model = new System_Models_Listtype;
        $sListTypeIdList = $this->_request->getParam('ListtypeIDList');
        echo Zend_Json::encode($model->deleteRows($sListTypeIdList));
        die();
    }

    public function cachefileAction() {
        $model = new System_Models_Listtype();
        $sListTypeIdList = $this->_request->getParam('ListtypeIDList');
        $aListTypeIdList = array();
        if ($sListTypeIdList) {
            $aListTypeIdList = explode(',', $sListTypeIdList);
        }
        $status = 1;
        $arrList = $model->getAll($aListTypeIdList,$status);
        // Thuc hien Tao file cache
        $strXmlItem = '';
        if($arrList){
            $cacheFile = new Clound_CacheFile();
            $cache = $cacheFile->cache();
            $listTypeCode = '';
            $count = sizeof($arrList);
            $arrTemp = array();
            $iTem = 0;
            for ($i = 0; $i < $count; $i++){
                if ($listTypeCode != $arrList[$i]['ListTypeCode']){
                    //Khởi tại lại mảng lưu dữ liệu tạm
                    $arrTemp = array(); $iTem = 0;
                    $listTypeCode = $arrList[$i]['ListTypeCode'];
                }
                $arrTemp[$iTem]['Code']       = $arrList[$i]['Code'];
                $arrTemp[$iTem]['Name']       = $arrList[$i]['Name'];
                //Lấy cột từ chuỗi XML
                $xmlData = $arrList[$i]['XmlData'];
                if ($xmlData){
                    $xmlData = '<?xml version="1.0" encoding="UTF-8"?><root>'.$xmlData.'</root>';
                    $objXmlData = new Zend_Config_Xml($xmlData,'data_list');
                    $arrXmlData = $objXmlData->toArray();
                    $arrKey = array_keys($arrXmlData); 
                    foreach ($arrKey As $value){
                        if (trim($arrXmlData[$value]) != ""){//Chỉ lấy giá những cột mà thẻ có giá trị 
                            $sColumnName = '' . $value . '';
                            $arrTemp[$iTem][$sColumnName]   = $arrXmlData[$value];
                        }
                    }
                }
                $iTem++;
                if ($count === $i+1 || $listTypeCode != $arrList[$i+1]['ListTypeCode']){
                    //Lưu nội dung vào cache
                    $cache->save($arrTemp,$listTypeCode);
                }
            }	
            
        }
        $this->getHelper('viewRenderer')->setNoRender();
    }

    public function dataAction(){
        $objListType = new System_Models_Listtype();
        $parames = $this->_request->getParams();
        $data = $objListType->getAllListtype($parames);
        echo Zend_Json::encode($data);
        die;
    }

    /**
     * @see : Thuc hien Them moi LOAI DANH Muc
     *
     */
    public function addAction() {
        if (!$this->_request->isXmlHttpRequest()) {
            return $this->_helper->redirector('index');
        }
		$this->view->addHelperPath('Clound/Xml/Helper/', 'Clound_Xml_Helper');
        $ListtypeID = $this->_request->getParam('ListtypeID');
        //Gan gia tri mac dinh cho cac Input
        $Listtype = new System_Models_Listtype();
        $model = $Listtype->selectRow($ListtypeID);
        // Session đơn vị
        $arrUnit = array();
        $arrUnit[0]['C_CODE'] ='QLB';
        $arrUnit[0]['C_NAME'] ='Quận A';
        $arrUnit[1]['C_CODE'] ='QB';
        $arrUnit[1]['C_NAME'] ='Quận B';
        $arrUnit[2]['C_CODE'] ='QC';
        $arrUnit[2]['C_NAME'] ='Quận C';
        $arrUnit[3]['C_CODE'] ='QD';
        $arrUnit[3]['C_NAME'] ='Quận D';            
        $_SESSION['DON_VI'] = $arrUnit;
        // File xml mô tả form
        $filexml = 'xml/system/quan_tri_danh_muc.xml';
        $xmlObj = new Clound_Xml($filexml,$model);
        $formObj = $xmlObj->formfieldGenerate();
        $this->view->form=$formObj;
    }
}

?>